package cn.student.service.impl;

import cn.student.config.web.DataGridResultInfo;
import cn.student.dao.StudentDaoMapper;
import cn.student.pojo.Student;
import cn.student.pojo.dto.StudentDTO;
import cn.student.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 毛苏宇
 * User: me
 * Date: 2020/6/24
 * Time: 21:51
 * Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDaoMapper studentDaoMapper;

    @Override
    public boolean remove(long userId) {
        return studentDaoMapper.delete(userId)>0?true:false;
    }

    @Override
    public DataGridResultInfo<StudentDTO> find(int page, int rows) {
        Page<StudentDTO> studentDTOS = PageHelper.startPage(page, rows);
        studentDaoMapper.select();
        DataGridResultInfo<StudentDTO> dataGridResultInfo=new DataGridResultInfo<>();
        dataGridResultInfo.setEndRow(studentDTOS.getEndRow());
        dataGridResultInfo.setPageNum(studentDTOS.getPageNum());
        dataGridResultInfo.setPages(studentDTOS.getPages());
        dataGridResultInfo.setPageSize(studentDTOS.getPageSize());
        dataGridResultInfo.setRows(studentDTOS.getResult());
        dataGridResultInfo.setStartRow(studentDTOS.getStartRow());
        dataGridResultInfo.setTotal(studentDTOS.getTotal());
        return dataGridResultInfo;
    }

    @Override
    public boolean add(Student student) {
        student.setUpdateTime(System.currentTimeMillis());
        return studentDaoMapper.insert(student)>0?true:false;
    }

    @Override
    public boolean modify(Student student) {
        student.setUpdateTime(System.currentTimeMillis());
        return studentDaoMapper.update(student)>0?true:false;
    }

    @Override
    public DataGridResultInfo<StudentDTO> findRegisterStudent(long cpId,int page,int rows) {
        Page<StudentDTO> studentDTOS = PageHelper.startPage(page, rows);
        studentDaoMapper.selectRegisterStudent(cpId);
        DataGridResultInfo<StudentDTO> dataGridResultInfo=new DataGridResultInfo<>();
        dataGridResultInfo.setEndRow(studentDTOS.getEndRow());
        dataGridResultInfo.setPageNum(studentDTOS.getPageNum());
        dataGridResultInfo.setPages(studentDTOS.getPages());
        dataGridResultInfo.setPageSize(studentDTOS.getPageSize());
        dataGridResultInfo.setRows(studentDTOS.getResult());
        dataGridResultInfo.setStartRow(studentDTOS.getStartRow());
        dataGridResultInfo.setTotal(studentDTOS.getTotal());
        return dataGridResultInfo;
    }

    @Override
    public StudentDTO findByUserId(long userId) {
        return studentDaoMapper.selectByUserId(userId);
    }

}
